<template>
    <h1>用户借款页面</h1>

    <table>
        <tr>
            <td>借款款项名称</td>
            <td>{{ Info.BM_Name }}</td>
        </tr>

        <tr>
            <td>最高金额</td>
            <td>{{ Info.BM_Money }}</td>
        </tr>

        <tr>
            <td>利息</td>
            <td>{{ Info.BI_Interest }}</td>
        </tr>

        <tr>
            <td>借款金额</td>
            <td>
                <input type="text" v-model="NMoney" >
            </td> 
        </tr>

        <tr>
            <td>每月还款金额</td>
            <td></td>
        </tr>

        <tr>
            <td colspan="2">
                <input  type="button" value="确认借款" @click="AddUserBorrow" >
            </td>
        </tr>

    </table>

</template>

<script setup lang="ts">

import { ref,onMounted } from 'vue';
import axios from 'axios';
import { useRoute,useRouter } from 'vue-router';
import {useUserStore} from '../../store/index.ts'

const route = useRoute();

const router = useRouter();

const userStore = useUserStore();

console.log(route.params.Id);//借款编号
console.log(userStore.UId)//用户编号

const Info = ref({
    BM_Name:'',
    BM_Money:'',
    BM_Year:'',
    BI_Interest:''
})

const NMoney =ref('');
//const NewMoney = ref('');

onMounted (()=>{
    axios({
        method:'get',
        url:'http://localhost:63517/api/Borrow/SearBorrowData',
        params:{
            Id:route.params.Id
        }
    })
    .then(res=>{
        console.log(res.data);
        Info.value = res.data[0];
    })
    .catch(err=>{
        console.log(err);
    })
})

const AddUserBorrow = ()=>{
    axios({
        method:'post',
        url:'http://localhost:63517/api/Borrow/AddUserBorrow',
        data:{
            U_Id:userStore.UId,
            BM_Id:route.params.Id,
            UB_Money:NMoney.value
        }
    })
    .then(res=>{
        if(res.data>0){
            alert('用户借款成功');
            router.push('/UserBorrShow');
        }
        else{
            alert('用户借款失败');
        }

    })
    .catch(res=>{
        console.log(res);
    })
}


</script>